package cn.jjxx.modules.sys.service.impl;

import java.util.*;

import cn.jjxx.core.common.service.impl.CommonServiceImpl;
import cn.jjxx.core.model.AjaxJson;
import cn.jjxx.core.query.wrapper.EntityWrapper;
import cn.jjxx.modules.sys.entity.SysHomePageManageUser;
import cn.jjxx.modules.sys.mapper.SysHomepageManageMapper;
import cn.jjxx.modules.sys.entity.SysHomepageManage;
import cn.jjxx.modules.sys.service.ISysHomePageManageUserService;
import cn.jjxx.modules.sys.service.ISysHomepageManageService;
import cn.jjxx.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author zcg
 * @version V1.0
 * @Title: 首页管理
 * @Description: 首页管理
 * @date 2018-12-07 16:56:31
 */
@Transactional
@Service("sysHomepageManageService")
public class SysHomepageManageServiceImpl extends CommonServiceImpl<SysHomepageManageMapper, SysHomepageManage> implements ISysHomepageManageService {

    @Autowired
    private ISysHomePageManageUserService iSysHomePageManageUserService;

    @Override
    public boolean insertBatch(List<SysHomepageManage> list) {
        if (list != null && list.size() > 0) {
            return baseMapper.insertBatch(list);
        }
        return true;
    }

    @Override
    public boolean updateBatch(List<SysHomepageManage> list) {
        if (list != null && list.size() > 0) {
            return baseMapper.updateBatch(list);
        }
        return true;
    }

    @Override
    public AjaxJson toSaveBind(List<String> userIds, String homePageId) {
        AjaxJson ajaxJson = new AjaxJson();
        ajaxJson.setRet(0);
        ajaxJson.setMsg("关联成功!");
        try {
            List<SysHomePageManageUser> list = new ArrayList<>();
            for (String s : userIds) {
                SysHomePageManageUser user = new SysHomePageManageUser();
                user.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                user.setCreateBy(UserUtils.getUser());
                user.setCreateDate(new Date());
                user.setUserId(s);
                user.setHomePageId(homePageId);
                list.add(user);
            }
            if (list.size() > 0) {
                iSysHomePageManageUserService.insertBatch(list);
            }
        } catch (Exception e) {
            e.printStackTrace();
            ajaxJson.setMsg("关联失败!");
            ajaxJson.setRet(1);
        }
        return ajaxJson;
    }

    @Override
    public AjaxJson toSaveCancel(List<String> userIds, String homePageId) {
        AjaxJson ajaxJson = new AjaxJson();
        ajaxJson.setRet(0);
        ajaxJson.setMsg("取消成功!");
        try {
            if (userIds.size() > 0) {
                Map<String, Object> map = new HashMap<>();
                map.put("list", userIds);
                map.put("homePageId", homePageId);
                baseMapper.toSaveCancel(map);
            }
        } catch (Exception e) {
            e.printStackTrace();
            ajaxJson.setMsg("取消失败!");
            ajaxJson.setRet(1);
        }
        return ajaxJson;
    }

    @Override
    public AjaxJson toSetDefault(String id) {
        AjaxJson ajaxJson = new AjaxJson();
        ajaxJson.setRet(0);
        ajaxJson.setMsg("设置成功!");
        try {
            //修改旧数据
            EntityWrapper<SysHomepageManage> entityWrapper = new EntityWrapper<>();
            entityWrapper.eq("is_default", "1");
            List<SysHomepageManage> list = selectList(entityWrapper);
            if (list.size() > 0) {
                SysHomepageManage sysHomepageMana = list.get(0);
                sysHomepageMana.setIsDefault("0");
                insertOrUpdate(sysHomepageMana);
            }
            SysHomepageManage sysHomePageManage = selectById(id);
            sysHomePageManage.setIsDefault("1");
            insertOrUpdate(sysHomePageManage);

        } catch (Exception e) {
            e.printStackTrace();
            ajaxJson.setMsg("设置失败!");
            ajaxJson.setRet(1);
        }
        return ajaxJson;
    }
}
